from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from .models import discussionThread,discussionPost,discussionStatus,userProfile
from django.utils import timezone
from .forms import feedbackForm, UserForm
# Create your views here.

def index(request):
    latest_thread_list = discussionThread.objects.order_by('-created')
    context = {'latest_thread_list': latest_thread_list}
    return render(request,'thediscussion/layouts/default.html',context)
def registeruser(request):
    if request.method == 'POST':
       userregisterform = UserForm(request.POST)
       if userregisterform.is_valid():
           userregisterform.save(commit=True)
           return HttpResponseRedirect('/thediscussion/success/')
    else:
        userregisterform = UserForm()
    return render(request,'thediscussion/registration.html',{'userregisterform':userregisterform})
def loginuser(request):
    username = request.POST['username']
    password = request.POST['password']
   	userlogin = authenticate(username=username,password=password)
	if user is not None:
	    if user.is_active:
	        login(request,userlogin)
			return HttpResponseRedirect(reverse('thediscussion:index', args=(discussionpost.id,)))
	else:
	    #Code to be written
def detail(request,discussionthread_id):
    discussionthread = get_object_or_404(discussionThread,pk=discussionthread_id)
    return render(request, 'thediscussion/detail.html', {'discussionthread': discussionthread})
def submit(request, discussionthread_id):
    discussionthread = get_object_or_404(discussionThread, pk=discussionthread_id)
    postContent = request.POST['thread_answer']
    if not postContent:
        return render(request, 'thediscussion/detail.html', {
            'discussionthread': discussionthread,
            'error_message': "We understand you are clueless. So are we. Please fill in something before you hit the submit button",
        })
    discussionpost = discussionPost(content=request.POST['thread_answer'], created = timezone.now(), thread_id = discussionThread.objects.get(pk=discussionthread_id), user_id = User.objects.get(pk=1),status = discussionStatus.objects.get(pk=2))
    discussionpost.save()
        # Always return an HttpResponseRedirect after successfully dealing
        # with POST data. This prevents data from being posted twice if a
        # user hits the Back button.
    return HttpResponseRedirect(reverse('thediscussion:detail', args=(discussionthread.id,)))
def ask(request, user_id):
    user_details = User.objects.get(pk=user_id)
    return render(request,'thediscussion/layouts/ask.html',{'user_details': user_details})
def postdiscussion(request, user_id):
    discussionpost = discussionThread(subject=request.POST['new_discussion_subject'],discussionbody=request.POST['new_discussion_body'], created = timezone.now(), user_id = User.objects.get(pk=user_id) ,status = discussionStatus.objects.get(pk=2), rating = 1)
    discussionpost.save()
    return HttpResponseRedirect(reverse('thediscussion:detail', args=(discussionpost.id,)))
def feedback(request):
    if request.method == 'POST':
       form = feedbackForm(request.POST)
       if form.is_valid():
           return HttpResponseRedirect('/thediscussion/thanks/')
    else:
        form = feedbackForm()
    return render(request,'thediscussion/feedback.html',{'form':form})
def thanks(request):
    return render(request,'thediscussion/thanks.html')
def success(request):
    return render(request,'thediscussion/success.html')